enyo.kind({
	name: "AssessmentUI_ReportRun",
	kind: "AssessmentUI_PanelBase",
	
	caption: "Assessment Report",
	submitLeftCaption: "",
	classes: "panel-body",
	
	runList: null,
	currentLoading: null,
	currentReportList: null,
	currentReportPayload: null,
	runPageCount: 1,
	runPageSize: 5,
	runPageNumber: 1,
	
	components: [
		{kind: "onyx.Groupbox", classes: "groupbox", components:[
			{kind:"onyx.GroupboxHeader",content:"Please Tab the Report to View",classes:"group-header"},
			{name: "runRepeater", kind: "Repeater", onSetupItem: "runRepeaterSetupItem", classes:"content", components:[
				{name: "runInfo", kind : "hpit.Divider", caption : "", collapsable: true, isExpanded: false, 
					checkable: false, onCollapseChange:"runInfoCollapseChange", style:"padding:10px 0px 10px 0px;", components : [
					{name: "runIDContent", content : "", style: "display: none"},
					{name: "statusCodeContent", content : "", style: "display: none"},
					{name: "runInfoContent", content: "", style: "padding: 0px 10px 10px 20px; font-weight: bold;", flex: 1},
					{name: "loadingContent", className: "divider-content"},
					{name: "reportRepeater", kind: "Repeater",  onSetupItem: "reportRepeaterSetupItem", style: "padding: 0px 10px 0px 10px", components: [
						{style: "padding: 10px;", classes:"reportRepeater-item", onclick: "itemClick", components: [
							{name: "reportIDContent", content: "cora" ,  style: "display: none"},
							{name: "reportInfoContent", content: "reportInfo" , flex: 1}
						]}
					]}
				]},
				{name: "moreButton", content : "Tap Here to Load More...", style: "margin: 5px 0px 5px 10px; font-weight: bold; font-style: italic;", onclick: "moreButtonClicked", showing: false}, 
			]}
		]},
	],
	
	create: function() {
		this.inherited(arguments);
		this.initPanel();
	},
	
	initPanel: function() {
		this.currentLoading = null;
		this.currentReportList = null;
		this.runList = null;
		this.currentReportPayload = null;
		
		//reset the controls display and status
		AssessmentUI_Data.selectedReportID = -1;
		
		//call the service
		var service = new hpit.WebService();
		service.setUrl(AssessmentUI_Config.getServiceUrl_RetrieveRunList());
		service.setHeaders(AssessmentUI_Config.getCommonServiceHeader());
		service.setParams({value0: "mark.davis2@hp.com", value1: this.runPageSize, value2: this.runPageNumber});
		service.call(this, "processResponse");
	},
	
	processResponse: function(inSender, inResponse) {
		if(inResponse.statusCode === 200) {
			if(this.runList == null) {
				this.runList = new Array();
			}
			
			if(inResponse.payload.analysisRunSBSList == null) {
				AssessmentUI_Data.parentWnd.showMsg("No item found");
			} else {
				for(var i = 0; i< inResponse.payload.analysisRunSBSList.length; i++) {
					this.runList.push(inResponse.payload.analysisRunSBSList[i]);
				}
			}
			
			//store the page count
			this.runPageCount = inResponse.payload.pageCount;
			
			//show device list
			this.$.runRepeater.count = this.runList.length;
			this.$.runRepeater.build();
			}
		else {
			AssessmentUI_Data.parentWnd.showMsg("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	runRepeaterSetupItem: function(inSender, inEvent) {
		if(this.runList == null) return;	//device list not loaded
		if(inEvent.item.parent.name == "reportRepeater") return;
		var inIndex = inEvent.index;
		var rowControl = inEvent.item;
		rowControl.$.runInfo.setCaption(" [Run#" + this.runList[inIndex].id + " Request#" + this.runList[inIndex].analysisRequestId + "] " + this.runList[inIndex].type + "; " + this.runList[inIndex].statusCode);;
		rowControl.$.runIDContent.setContent(this.runList[inIndex].id);
		rowControl.$.statusCodeContent.setContent(this.runList[inIndex].statusCode);
		rowControl.$.runInfoContent.setContent(this.runList[inIndex].statusDefaultDescription);
		rowControl.$.reportRepeater.build();
			
		if (inIndex < this.runList.length - 1) {
		
			rowControl.$.runInfo.setShowing(true);
			rowControl.$.moreButton.setShowing(false);
			this.$.runRepeater.render();
		
			return true;
		}
		else if(inIndex == (this.runList.length - 1) && this.runPageNumber < this.runPageCount && inIndex != 0) {
			var rowControl = inEvent.item;
			rowControl.$.moreButton.setShowing(true);
			rowControl.$.runInfo.setShowing(false);
			this.$.runRepeater.render();
			return true;
		}
	},
	
	runInfoCollapseChange: function(inSender,inEvent) {
		var rowControl = inSender.parent;
		if(inSender.isExpanded){
			var statusCode = rowControl.$.statusCodeContent.content;
			if(statusCode == "COMPLETED")
			{
				this.currentLoading = rowControl.$.loadingContent;
				this.currentReportList = rowControl.$.reportRepeater;
				this.currentLoading.setContent("Loading...");
				
				//call the service
				var service = new hpit.WebService();
				service.setUrl(AssessmentUI_Config.getServiceUrl_RetrieveReportList());
				service.setHeaders(AssessmentUI_Config.getCommonServiceHeader());
				service.setParams({value0: rowControl.$.runIDContent.content});
				service.call(this, "reportListServiceGot");
			}
		}
	},
	reportListServiceGot: function(inSender, inResponse) {
		if(inResponse.statusCode === 200) {
			if(inResponse.payload.length <= 0) {
				AssessmentUI_Data.parentWnd.showMsg("No item found");
			} else {
				this.currentReportPayload = inResponse.payload;
			}
			
			//show list
			this.currentReportList.count = inResponse.payload.length;
			this.currentReportList.build();
			this.currentLoading.setContent("");
			this.currentLoading.render();
		}
		else {
			AssessmentUI_Data.parentWnd.showMsg("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	reportRepeaterSetupItem: function(inSender, inEvent) {
		if(this.currentReportPayload == null) return;
		
		var inIndex = inEvent.index;
		var rowControl = inEvent.item;
		
		if(inIndex < this.currentReportPayload.length) {
			rowControl.$.reportIDContent.setContent(this.currentReportPayload[inIndex].saDocKey);
			rowControl.$.reportInfoContent.setContent(this.currentReportPayload[inIndex].name + " [" + this.currentReportPayload[inIndex].language + "]");
			this.currentReportList.render();
			return true;
		}
	
	},
	
	itemClick: function(inSender, inEvent){
		var inIndex = inSender.parent.index;
		AssessmentUI_Data.selectedReportID = this.currentReportPayload[inIndex].saDocKey;
		AssessmentUI_Data.parentWnd.switchView("report_detail");
	},
	
	moreButtonClicked: function() {
		this.runPageNumber++;
		
		var service = new hpit.WebService();
		service.setUrl(AssessmentUI_Config.getServiceUrl_RetrieveRunList());
		service.setHeaders(AssessmentUI_Config.getCommonServiceHeader());
		service.setParams({value0: "mark.davis2@hp.com", value1: this.runPageSize, value2: this.runPageNumber});
		service.call(this, "processResponse");
	}
});